package entity;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;

import constans.Globals;
import controller.ObjectController;
import engine.Map;
import engine.MapMatrix;

public class Bullet extends Entity{

	int speed = 1;
	float angle;
	Unit prey;
	int damage;
	int range;
	int splash;
	float distance = 0;
	float startX;
	float startY;
	boolean isDead;
	boolean detonate;
	boolean rotate;
	float imgX;
	float maxX = 180;

	public Bullet(float x, float y,Unit prey,  int damage, int range,int splash, Image img, boolean rotate) {
		super();
		this.x = x;
		this.y = y;
		this.prey = prey;
		this.damage = damage;
		this.range = range;
		this.splash = splash;
		startX = x;
		startY = y;
		setAngle();
		type = Entity.BULLET;
		this.img = img;
		this.rotate = rotate;
		this.imgX = 0;
	}
	
	public void render(GameContainer container, Graphics g) throws SlickException
	{	
		img.setCenterOfRotation(15, 15);
		if(rotate)
			img.rotate((float) Math.toDegrees(angle));
		img.draw(Map.dx + x, y, Map.dx + x + size, y + size, imgX, 0, imgX + size, size);
		if(rotate)
			img.rotate((float) Math.toDegrees(-angle));
	}
	public void update(GameContainer container, int delta) throws SlickException
	{
		time();
		
		if(detonate)
		{
			if(time %7 == 0)
			{
				
				imgX += 30;
				if(imgX == maxX)
					isDead = true;		
			}
			return;
		}
		//if samonaprowadzajaca
		setAngle();
		x+= speed * Math.sin(angle);
		y-= speed * Math.cos(angle);
		
		distance =(float) Math.sqrt( Math.pow(x - startX,2) + Math.pow(y - startY,2));
		
		if(Math.abs((prey.getX() - x)) < 10 && Math.abs(prey.getY() - y) < 10 )
		{
			ObjectController.takeDamage(prey, damage);
			
			if(splash > 0) findPreyToSplash();
			detonate = true;				
		}
		if(y > Globals.MAP_SIZE_Y || y < 0 || x > Globals.MAP_SIZE_X || x < 0) isDead = true;
	}
	public boolean isDead()
	{
		return isDead;
	}
	private void setAngle()
	{
		if((int)prey.y == (int)y) return;
		if((int)prey.y > (int)y)
			angle = (float) (Math.toRadians(-90) - Math.acos((x - prey.getX()) / Math.sqrt(Math.pow(y - prey.getY(), 2) + Math.pow(x - prey.getX(),2))));
		else
			angle = (float) (Math.toRadians(-90) + Math.acos((x - prey.getX()) / Math.sqrt(Math.pow(y - prey.getY(), 2) + Math.pow(x - prey.getX(),2))));
	}
	private void findPreyToSplash()
	{
		MapMatrix map = ObjectController.getMap();

		i = (int) (this.x/Globals.FIELD_SIZE);
		j = (int) (this.y/Globals.FIELD_SIZE);
		
		for(int i = this.i - splash ; i <= this.i + splash ; i++)
			for(int j = this.j - splash ; j <= this.j + splash ; j++)
				if(isHerePrey(i,j,map))
					if(map.getEntity(i, j) != prey)
						ObjectController.takeDamage((Unit)map.getEntity(i, j), damage/3);
	}
	private boolean isHerePrey(int i1, int j1, MapMatrix map)
	{			
		if(i1 >= 0 && i1 < Globals.MAP_I && j1 >= 0 && j1 < Globals.MAP_J)
			//dodaje 0.5, zeby zaokraglic zasieg
			if((float)splash + 0.5f >= Math.sqrt( Math.pow(i - i1,2) + Math.pow(j - j1,2)))
				if(!map.isFieldEmpty(i1, j1))
					if(map.getEntity(i1, j1).getType() == Entity.UNIT)
						return true;
		return false;
	}
	

}
